package com.nowcoder.community.controller;

import com.nowcoder.community.config.RedisConfig;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.GenerateUtil;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.controller
 * @Author: Master_XXJS
 * @CreateTime: 2023-01-04  10:53
 * @Description: TODO
 * @Version: 1.0
 */
@Controller
public class LikeController implements CommunityConstant {

    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LikeService likeService;
    @Autowired
    private EventProducer eventProducer;

    /**
     * 页面点击 点赞 之后，需要的处理
     * 返回页面 用户对某实体 点赞 的详细情况
     * @param entityType 实体类型
     * @param entityId 实体id
     * @param postId 帖子id
     * @return
     */
    @RequestMapping(path = "/like",method = RequestMethod.POST)
    @ResponseBody
    public String like(int entityType,int entityId,int entityUserId,int postId){
        //用户点击 点赞
        // 添加 用户id（数量）到 redis 数据库中
        likeService.like(hostHolder.getUser().getId(),entityType,entityId,entityUserId);

        //查询 某实体点赞的总数量
        long entityLikeCount = likeService.findEntityLikeCount(entityType, entityId);
        //查询 用户对某实体点赞的状态
        int entityLikeStatus = likeService.findEntityLikeStatus(hostHolder.getUser().getId(), entityType, entityId);

        //进行返回数据给前端
        Map<String, Object> map = new HashMap<>();
        map.put("likeCount",entityLikeCount);
        map.put("likeStatus",entityLikeStatus);

        //触发点赞事件，需要点赞的地方的跳转路径，方便知道是哪的点赞
        if (entityLikeStatus == 1){
            Event event = new Event()
                    .setTopic(TOPIC_LIKE)
                    .setUserId(hostHolder.getUser().getId())
                    .setEntityType(entityType)
                    .setEntityId(entityId)
                    .setEntityUserId(entityUserId)
                    .setData("postId",postId);
            eventProducer.fireEvent(event);
        }

        if (entityType == ENTITY_TYPE_POST){
            //计算帖子的分数
            String redisKey = RedisKeyUtil.getPostKey();
            redisTemplate.opsForSet().add(redisKey,postId);
        }

        //返回 json 数据到前端（map——》json）
        return GenerateUtil.getJsonString(0,null,map);
    }
}

